package com.xcc.common.result;

import com.xcc.common.code.ErrorCode;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Map;

/**
 * @name: frosty
 * @description : 返回体
 * @date: 2019/11/8
 */
@Component
public class ResultBody<T> implements Serializable {
    @Value("${server.port}")
    private  int port;
    @ApiModelProperty("响应编码:200 请求处理成功")
    private int code=200;
    @ApiModelProperty("提示消息")
    private String message="请求成功";
    @ApiModelProperty("响应数据")
    private T data;
    @ApiModelProperty("额外数据")
    private Map<String ,Object> extra;
    @ApiModelProperty("请求状态")
    private boolean status=true;

    private ResultBody(int code, String message, T data, Map<String, Object> extra, boolean status) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.extra = extra;
        this.status = status;

    }

    private ResultBody(int code, String message, T data, boolean status) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.status = status;
    }

    private ResultBody(int code, String message, boolean status) {
        this.code = code;
        this.message = message;
        this.status = status;
    }



    public ResultBody() {

    }



    public  static ResultBody success(){
        return  new ResultBody();
    }

    public  static  <T> ResultBody<T> success(T data){
       return    new ResultBody().setData(data);
    }


    public  static ResultBody error(String message){
     return    new ResultBody<>(ErrorCode.FAILED.getCode(),message,false);
    }
    public  static ResultBody error(int code, String message){
        return    new ResultBody<>(code,message,false);
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public ResultBody<T> setMessage(String message) {
        this.message = message;
        return    this;
    }

    public T getData() {
        return data;
    }

    public ResultBody setData(T data) {
        this.data = data;
        return this;

    }

    public Map<String, Object> getExtra() {
        return extra;

    }

    public ResultBody<T> setExtra(Map<String, Object> extra) {
        this.extra = extra;
        return this;
    }

    public boolean getStatus() {
        return status;
    }

    public ResultBody setStatus(boolean status) {
        this.status = status;
     return     this;
    }

    public int getPort() {
        return port;
    }

    public ResultBody setPort(int port) {
        this.port = port;
        return  this;
    }
}
